/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
	January 7th, 2021

	DESCRIPTION: Creates Dataset that Allows to Check for
    Multicolinearity in Temp Agencies

*******************************************************************************/



/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/07_Multicolinearity_Check`curr_date'", replace


/********************************************************************************
***** Load Clean Dataset
********************************************************************************/

use "${intermediate_data_emp}/full_Dataset_Clean.dta", clear


*******

	* Generate the variable for an extra Fixed Effect for the temp agencies
	gen double temp_agency_id = cuit_empl if temp_worker == 1
	replace temp_agency_id = 999999999998 if temp_worker == 0

    *Drop Unecessary variables
    foreach var of varlist month year sueldo sac DOB ever_public_worker temp_duplicate modalidad ciiu_4 {
        cap drop `var'
    }

	*Drop missing observations
	drop if log_real_wage == . | temp_worker == . | cuil_trab == . | cuit_empl == . |  date == . | temp_agency_id == .

*******

** Check which firms can potentially generate collinearity

    *Check the user firms that have relations with only 1 temp agency
	unique temp_agency_id if temp_agency_id != 999999999998, by(real_firm) gen(unique2)

	*We want to make sure that we don't have cases in which the temp agency has relations with a number X of user firms but all these user firms ONLY have relations with this temp agency.
	gen temp_agency_unique = temp_agency_id if unique2 == 1
	gen total = 1

	gcollapse (sum) total, by(temp_agency_unique)
	drop if temp_agency_unique == .

	rename temp_agency_unique temp_agency_id
	save "${intermediate_data_emp}/multicolinearity_check_akm4.dta", replace

log close
